package estyle.auto.nkornilova.util;

import estyle.auto.nkornilova.model.Message;
import estyle.auto.nkornilova.model.XMLMessages;
import nu.xom.*;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nkornilova
 * Date: 07.05.13
 * Time: 10:18
 * To change this template use File | Settings | File Templates.
 */
public class BankAccountMessageXMLParser {

    public Message getPaperAccountMessageFromParser(String xmlFileName) throws ParsingException, IOException {
        InputStream ins = BankAccountMessageXMLParser.class.getClassLoader()
                .getResourceAsStream(xmlFileName);

        Builder builder = new Builder();
        Document doc = builder.build(ins);
        Element rootElement = doc.getRootElement();
        Elements messageElements = rootElement.getChildElements();
        Element messageElement = null;
        Element messageElementProperty = null;
        for(int i=0;i<messageElements.size();i++)
        {
            Message message = new Message();
            messageElement = messageElements.get(i);

        }
        return null;
    }

    public List<Message> getMessagesFromXML(String pathXMLFile)
    {
        try {

            Unmarshaller u = (JAXBContext.newInstance(XMLMessages.class)).createUnmarshaller();
            return  ((XMLMessages)u.unmarshal
                    (new FileInputStream((new File(".").getCanonicalPath()) + pathXMLFile))).messageList;

        } catch( JAXBException je ) {
            je.printStackTrace();
        } catch( IOException ioe ) {
            ioe.printStackTrace();
        }
        return new ArrayList<Message>();
    }
}
